//
// Created by 陈宇 on 2017/4/26.
// Copyright (c) 2017 科匠武汉. All rights reserved.
//

import Foundation
import ObjectMapper

class BaseBean: NSCoding, ObjectMapper.Mappable {

    public static let INVALID_ID: Int = -1

    var id: Int = INVALID_ID
    var title: String = ""
    
    init() {}
    
    required init?(map: Map) { }
    
    /**
     * NSCoding required initializer.
     * Fills the data from the passed decoder
     */
    required init?(coder aDecoder: NSCoder) {
        id = (aDecoder.decodeObject(forKey: "id") as? Int) ?? BaseBean.INVALID_ID
        title = (aDecoder.decodeObject(forKey: "title") as? String) ?? ""
    }
    
    func mapping(map: Map) {
        id <- map["id"]
        title <- map["title"]
    }
    
    /**
     * NSCoding required method.
     * Encodes mode properties into the decoder
     */
    public func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(title, forKey: "title")
    }

    public var beanName: String {
        return "BaseBean"
    }

}

extension BaseBean: CustomDebugStringConvertible {
    public var debugDescription: String {
        return "\(beanName): \(self.toJSON())"
    }
}

